package com.sos.service;

import java.util.ArrayList;

import com.sos.vo.Contactor;



import android.content.Context;
import android.database.Cursor;
import android.provider.Contacts;
import android.provider.Contacts.People;
import android.provider.Contacts.Phones;
import android.provider.ContactsContract;
import android.telephony.PhoneNumberUtils;
import android.util.Log;
/**
 * 获取通讯录记录列表
 * ***/
public class SysContactorManager {
	private final static String TAG = SysContactorManager.class.getSimpleName();
	
	private Context context;
	
	public SysContactorManager(Context context){
		Log.i(TAG, "SysContactorManager");
		
		this.context = context;
	}
	
	
	public ArrayList<Contactor> getList(){
		Log.i(TAG, "getList");
		
		ArrayList<Contactor> list = new ArrayList<Contactor>();
		
		String[] columns = new String[] {ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.Contacts.HAS_PHONE_NUMBER,ContactsContract.Contacts._ID};
		//String where = "NOT "+ Phones.NUMBER + " ISNULL";
		Log.i(TAG, Phones.CONTENT_URI.toString());
		Log.i(TAG, ContactsContract.Contacts.CONTENT_URI.toString());
		Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, columns, null, null,null);
		String number = "";

		while (cursor.moveToNext()) {
			Contactor bean = new Contactor();
			bean.setName(cursor.getString(0));
			//bean.setPhone(cursor.getString(1));
			String contactID =cursor.getString(2);
			String hasPhone =cursor.getString(1);
			if(hasPhone.compareTo("1") == 0){
				Cursor phones = context.getContentResolver().query(
						ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
						ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactID, null, null);
				while (phones.moveToNext()) {
					String phoneNumber = phones .getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
					String phoneTpye = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
					number += phoneNumber + "\n"; 
					Log.d(TAG,"testNum="+ phoneNumber + "type:"+phoneTpye);
					bean.setPhone(phoneNumber);
					}
				phones.close();


			}
			list.add(bean);
			
			Log.d(TAG, "name:" + bean.getName());
			Log.d(TAG, "phone:" + bean.getPhone());
		}
		cursor.close();
		
		return list;
	}
	
	public boolean isPhoneExisted(String phone){
		Log.i(TAG, "isPhoneExisted");
		
		return getContactor(phone)!=null;
	}
	/**
	 * 获取对象
	 * @param phone
	 * @return
	 */
	public Contactor getContactor(String phone){
		Log.i(TAG, "getContactor");
		
		String formattedNumber = PhoneNumberUtils.formatNumber(phone);
		Log.d(TAG, "formatted number:" + formattedNumber);
		
		Contactor bean = null;
		String[] columns = new String[] {Phones.NAME, Phones.NUMBER};
		String where = Phones.NUMBER + "=? or " + Phones.NUMBER + "=?";
		String[] whereArgs = {phone, formattedNumber};
		
		Cursor cursor = context.getContentResolver().query(Phones.CONTENT_URI, columns, where, whereArgs, null);
		if (cursor.moveToNext()) {
			bean = new Contactor();
			bean.setName(cursor.getString(0));
			bean.setPhone(cursor.getString(1));
			Log.d(TAG, "name:" + bean.getName());
			Log.d(TAG, "phone:" + bean.getPhone());
		}
		cursor.close();
		
		return bean;
	}
}
